package com.efast.cafe.portal.service.company.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.base.BaseCompanyInfoBean;
import com.efast.cafe.portal.dao.base.BaseCompanyInfoDao;
import com.efast.cafe.portal.dao.company.PortalCompanyPositionDao;
import com.efast.cafe.portal.entity.company.PortalCompanyOrg;
import com.efast.cafe.portal.entity.company.PortalCompanyPosition;
import com.efast.cafe.portal.entity.company.PortalCompanyUserOrg;
import com.efast.cafe.portal.entity.company.PortalCompanyUserOrgDto;
import com.efast.cafe.portal.entity.company.PortalCompanyUserSettingDto;
import com.efast.cafe.portal.service.company.IPortalCompanyUserOrgService;

/******************************************************************************
 * @File name : PortalCompanyUserOrgServiceImpl.java
 * 
 * @Author : xl
 * 
 * @Date : 2016-9-1
 * 
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-9-1 上午11:34:07 xl 1.0
 *            Initial Version
 *****************************************************************************/

@Service
@Transactional
public class PortalCompanyUserOrgServiceImpl extends CafeServiceImpl<PortalCompanyUserOrg> implements IPortalCompanyUserOrgService {
	
	@Qualifier("jdbcDao")
	@Autowired
	private  JdbcDao  jdbcDao;
	
	private PortalCompanyPositionDao portalCompanyPositionDao;
	private BaseCompanyInfoDao  baseCompanyInfoDao;
	
	
	
	@Autowired
	public void setBaseCompanyInfoDao(BaseCompanyInfoDao baseCompanyInfoDao) {
		this.baseCompanyInfoDao = baseCompanyInfoDao;
	}


	@Autowired
	public void setPortalCompanyPositionDao(PortalCompanyPositionDao portalCompanyPositionDao) {
		this.portalCompanyPositionDao = portalCompanyPositionDao;
	}


	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-9-1
	 * @see org.eae.eldf.portal.remote.company.PortalCompanyUserOrgService#getUserSetting()
	 **/
	@Override
	public PortalCompanyUserSettingDto getUserSetting() {
		//获取用户默认职位
		PortalCompanyUserSettingDto portalCompanyUserSettingDto = new PortalCompanyUserSettingDto();
		Criteria criteria = Criteria.create(PortalCompanyUserOrg.class)
				.where(PortalCompanyUserOrg.userName, new Object[] { SpringWebUtils.getRequestUser() })
				.and(PortalCompanyUserOrg.companyCode, new Object[] { SpringWebUtils.getRequestCompany() })
				.and(PortalCompanyUserOrg.isDefault, new Object[] { 1L });
		List<PortalCompanyUserOrg> listOrg = jdbcDao.queryList(criteria);
		if (listOrg.size() > 0) {
			PortalCompanyUserOrg portalCompanyUserOrg = listOrg.get(0);
			portalCompanyUserSettingDto.setPortal_company_user_default_Position_id(portalCompanyUserOrg
					.getPortal_company_position_id());
		}
		List<PortalCompanyPosition> listPosition = portalCompanyPositionDao.queryByUserHasPosition(
				SpringWebUtils.getRequestUser(), SpringWebUtils.getRequestCompany());
		//获取用户相关职位tree
		Map<String, PortalCompanyUserOrgDto> data=new HashMap<String, PortalCompanyUserOrgDto>();
		
		for (PortalCompanyPosition portalCompanyPosition : listPosition) {
			boolean isFirstOrg=false;
			PortalCompanyUserOrgDto  positionDto=new PortalCompanyUserOrgDto();
			positionDto.setId("position_"+portalCompanyPosition.getPortal_company_position_id());
			positionDto.setName(portalCompanyPosition.getPosition_name());
			positionDto.setParentId("org_"+portalCompanyPosition.getPortal_company_org_id());
			positionDto.setIconSkin("position");
			positionDto.setNocheck(false);
			if(portalCompanyUserSettingDto.getPortal_company_user_default_Position_id()!=null &&
					portalCompanyPosition.getPortal_company_position_id()!=null &&
					portalCompanyUserSettingDto.getPortal_company_user_default_Position_id().intValue()
					==portalCompanyPosition.getPortal_company_position_id().intValue()){
				positionDto.setChecked(true);
			}else{
				positionDto.setChecked(false);
			}
			data.put(positionDto.getId(), positionDto);
			Long orgId=portalCompanyPosition.getPortal_company_org_id();
			while(!isFirstOrg){
				Criteria orgCriteria=Criteria.create(PortalCompanyOrg.class).where(PortalCompanyOrg.portalCompanyOrgId, new Object[]{orgId});
				List<PortalCompanyOrg> listOrgDto= jdbcDao.queryList(orgCriteria);
				if(listOrgDto.size()>0){
					PortalCompanyOrg portalCompanyOrg=listOrgDto.get(0);
					PortalCompanyUserOrgDto  orgDto=new PortalCompanyUserOrgDto();
					orgDto.setId("org_"+portalCompanyOrg.getPortal_company_org_id());
					orgDto.setName(portalCompanyOrg.getOrg_name());
					orgDto.setParentId("org_"+portalCompanyOrg.getParent_id());
					orgDto.setIconSkin("org");
					orgDto.setNocheck(true);
					orgDto.setChecked(false);
					data.put(orgDto.getId(), orgDto);
					orgId=portalCompanyOrg.getParent_id();
					
				}else{
					isFirstOrg=true;	
				}
				
			}
			
		}
		BaseCompanyInfoBean companyInfo=baseCompanyInfoDao.queryByCompanyCode(SpringWebUtils.getRequestCompany());
		PortalCompanyUserOrgDto companyDto=new PortalCompanyUserOrgDto();
		companyDto.setId("org_"+0);
		companyDto.setParentId("company_"+companyInfo.getCompanyName_zh_CN());
		companyDto.setName(companyInfo.getCompanyName_zh_CN());
		companyDto.setIconSkin("company");
		companyDto.setNocheck(true);
		companyDto.setChecked(false);
		data.put(companyDto.getId(), companyDto);
		List<PortalCompanyUserOrgDto> companyUserOrgDtos=new ArrayList<PortalCompanyUserOrgDto>();
		 Set<String> set = data.keySet();
		 for (String key : set) {
			 companyUserOrgDtos.add(data.get(key));
		}
		
		portalCompanyUserSettingDto.setPortal_company_user_org(companyUserOrgDtos);
		
		return portalCompanyUserSettingDto;
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-9-1
	 * @see org.eae.eldf.portal.remote.company.PortalCompanyUserOrgService#saveUserSetting(org.eae.eldf.portal.po.company.PortalCompanyUserSettingDto)
	 **/
	@Override
	public String saveUserSetting(PortalCompanyUserSettingDto portalCompanyUserSettingDto) {
		if (portalCompanyUserSettingDto.getPortal_company_user_default_Position_id() != -1L) {
			Criteria criteria = Criteria.create(PortalCompanyUserOrg.class).set(PortalCompanyUserOrg.isDefault, 0L)
					.where(PortalCompanyUserOrg.userName, new Object[] { SpringWebUtils.getRequestUser() })
					.and(PortalCompanyUserOrg.companyCode, new Object[] { SpringWebUtils.getRequestCompany() });
			jdbcDao.update(criteria);
			PortalCompanyPosition portalCompanyPosition=portalCompanyPositionDao.queryPositionById(portalCompanyUserSettingDto.getPortal_company_user_default_Position_id());
			Criteria update = Criteria
					.create(PortalCompanyUserOrg.class)
					.set(PortalCompanyUserOrg.isDefault, 1L)
					.where(PortalCompanyUserOrg.userName, new Object[] { SpringWebUtils.getRequestUser() })
					.and(PortalCompanyUserOrg.companyCode, new Object[] { SpringWebUtils.getRequestCompany() })
					.and(PortalCompanyUserOrg.portalCompanyPositionId,
							new Object[] { portalCompanyUserSettingDto.getPortal_company_user_default_Position_id() }).and(PortalCompanyUserOrg.portalCompanyOrgId, new Object[]{portalCompanyPosition.getPortal_company_org_id()});
			jdbcDao.update(update);
		}

		return "success";
	}
}
